home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / cmplibsr.zoo / $translcuts1.P < prev   
Text File  |  1989-03-18  |  8KB  |  256 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona,1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25.  
  26. /* translcuts1.P contains routines for source-to-source transformation of
  27.    clauses during preprocessing.                      */
  28.  
  29. /* **********************************************************************
  30. $translcuts1_export([$transl_cuts/5,$transl_softcuts/5,$find_prag/4]).
  31.  
  32. $translcuts1_use($meta,[$functor/3,$univ/2,$length/2]).
  33. $translcuts1_use($aux1,[_,_,_,_,$umsg/1,_,_,$gensym_pred/2,_]).
  34. $translcuts1_use($blist,[$append/3,$member/2,_]).
  35. $translcuts1_use($tindex1,[$tindex/2]).
  36. $translcuts1_use($cond1,[$cond/5,_,_,$inline_neg/2]).
  37. ********************************************************************** */
  38.  
  39. $transl_cuts(P,N,Clauses,PragList,[PredDef1 | PredDefRest]) :-
  40.     $functor(Head,P,N),
  41.     N1 is N + 1,
  42.     $gensym_pred(P,NP),
  43.     $functor(NLit,NP,N1),
  44.     $copy_args(Head,NLit,N),
  45.     arg(N1,NLit,B),
  46.     $transl_cut_clauses(NP,Clauses,NewCls),
  47.     ((symtype('_$mode'(_,_,_),MDef), MDef > 0, '_$mode'(P,N,Mode)) ->
  48.          ($append(Mode,[2],NMode),
  49.           assert('_$mode'(NP,N1,NMode))
  50.          ) ;                  /* new pred inherits mode */
  51.          true
  52.     ),
  53.     $find_prag(P,N,PragList,Prag),
  54.     PredDef1 = pred(P,N,1,1,[rule(Head,('_$savecp'(B),NLit),0,0)],Prag),
  55.     PredDef2 = pred(NP,N1,1,1,NewCls,Prag),
  56.     $tindex(PredDef2,PredDefRest).
  57.  
  58. $transl_softcuts(P,N,Call,TCall,NCall) :-
  59.     var(Call) ->
  60.         (
  61.         ((NCall =:= 0, $translcuts_naked_var_msg(P,N)) ;
  62.          (NCall =\= 0)
  63.         ),
  64.         TCall = ('_$savecp'(X), '_$interp'(Call,X))
  65.         ) ;
  66.         $transl_softcuts1(P,N,Call,TCall,NCall).
  67.  
  68. :- mode($transl_softcuts1,5,[c,c,nv,d,d]).
  69. :- index($transl_softcuts1,5,3).
  70.  
  71. $transl_softcuts1(P,N,','(A,B), ','(NA,NB),Call) :- !,
  72.     $transl_softcuts(P,N,A,NA,Call),
  73.     $transl_softcuts(P,N,B,NB,Call).
  74. $transl_softcuts1(P,N,'->'(A,B),NB,Call) :-
  75.     !,
  76.     $transl_cuts_ifthenelse(P,N,CutPt,CutPt,A,B,fail,NB,Call).
  77. $transl_softcuts1(P,N,';'('->'(A,B),C),NB,Call) :-
  78.     $transl_cuts_ifthenelse(P,N,CutPt,CutPt,A,B,C,NB,Call).
  79. $transl_softcuts1(P,N,';'(A,B),if_then_else(Test,TP,FP),Call) :-
  80.     $cond(A,B,Test,TP0,FP0),
  81.     !,
  82.     $transl_softcuts(P,N,TP0,TP,Call),
  83.     $transl_softcuts(P,N,FP0,FP,Call).
  84. $transl_softcuts1(P,N,';'(A,B), ';'(NA,NB),Call) :- !,
  85.     $transl_softcuts(P,N,A,NA,Call),
  86.     $transl_softcuts(P,N,B,NB,Call).
  87. $transl_softcuts1(P,N,not(Goal), NewGoal,Call) :- !,
  88.     (
  89.      ($inline_neg(Goal, NewGoal)) ;
  90.      (NewGoal = ('_$savecp'(B),((NegGoal,'_$cutto'(B),fail) ; true)),
  91.       $transl_softcuts(P,N,Goal, NegGoal,Call)
  92.      )
  93.     ).
  94. $transl_softcuts1(P,N,call(Goal),NewGoal,_) :-
  95.     !,
  96.     $transl_softcuts(P,N,Goal,NewGoal,1).
  97. $transl_softcuts1(_,_,L,$consult_list(L), _) :- L = [_|_], !.
  98. $transl_softcuts1(P,N,Goal,Goal,_).
  99.  
  100. :- index($transl_cut_clauses,3,2).
  101.  
  102. $transl_cut_clauses(_,[],[]).
  103. $transl_cut_clauses(P,[C1|CRest],[NC1|NCRest]) :-
  104.     $transl_cut_clauses1(P,C1,NC1),
  105.     $transl_cut_clauses(P,CRest,NCRest).
  106.  
  107. :- mode($transl_cut_clauses1,3,[c,nv,d]).
  108. :- index($transl_cut_clauses1,3,2).
  109.  
  110. $transl_cut_clauses1(P,fact(Fact,CFlag),fact(NewFact,CFlag)) :-
  111.     $arity(Fact,N),
  112.     N1 is N + 1,
  113.     $functor(NewFact,P,N1),
  114.     $copy_args(Fact,NewFact,N).
  115. $transl_cut_clauses1(P,rule(H,B,CFlag0,CFlag1),rule(NH,NB,CFlag0,CFlag1)) :-
  116.     $arity(H,N),
  117.     N1 is N + 1,
  118.     $functor(NH,P,N1),
  119.     $copy_args(H,NH,N),
  120.     arg(N1,NH,V),
  121.     $transl_hardcuts(P,N,V,B,NB,0).
  122.  
  123. $transl_hardcuts(P,N,V,Call,TCall,NCall) :-
  124.     var(Call) ->
  125.         (
  126.             ((NCall =:= 0, $translcuts_naked_var_msg(P,N)) ;
  127.          (NCall =\= 0)
  128.         ),
  129.         TCall = ('_$savecp'(X), '_$interp'(Call,X))
  130.         ) ;
  131.         $transl_hardcuts1(P,N,V,Call,TCall,NCall).
  132.  
  133. :- mode($transl_hardcuts1,6,[c,c,d,nv,d,d]).
  134. :- index($transl_hardcuts1,6,4).
  135.  
  136. $transl_hardcuts1(P,N,V,','(A1,B1),','(A2,B2),Call) :-
  137.     !,
  138.     $transl_hardcuts(P,N,V,A1,A2,Call),
  139.     $transl_hardcuts(P,N,V,B1,B2,Call).
  140. $transl_hardcuts1(P,N,V,'->'(A,B),NB,Call) :-
  141.     $transl_cuts_ifthenelse(P,N,_,V,A,B,fail,NB,Call).
  142. $transl_hardcuts1(P,N,V,';'('->'(A,B),C),NB,Call) :-
  143.     $transl_cuts_ifthenelse(P,N,_,V,A,B,C,NB,Call).
  144. $transl_hardcuts1(P,N,V,';'(A1,B1),if_then_else(Test,TP,FP),Call) :-
  145.     $cond(A1,B1,Test,TP0,FP0),
  146.     !,
  147.     $transl_hardcuts(P,N,V,TP0,TP,Call),
  148.     $transl_hardcuts(P,N,V,FP0,FP,Call).
  149. $transl_hardcuts1(P,N,V,';'(A1,B1),';'(A2,B2),Call) :-
  150.     !,
  151.     $transl_hardcuts(P,N,V,A1,A2,Call),
  152.     $transl_hardcuts(P,N,V,B1,B2,Call).
  153. $transl_hardcuts1(P,N,V,not(Goal),NewGoal,Call) :-
  154.     !,
  155.     (
  156.      ($inline_neg(Goal, NewGoal)) ;
  157.      (NewGoal = ('_$savecp'(B),((NegGoal,'_$cutto'(B),fail) ; true)),
  158.       $transl_hardcuts(P,N,B,Goal, NegGoal,Call)
  159.      )
  160.     ).
  161. $transl_hardcuts1(P,N,V,'!','_$cutto'(V),_) :- !.
  162. $transl_hardcuts1(P,N,V,call(Goal),NewGoal,_) :-
  163.     !,
  164.     $transl_hardcuts(P,N,V,Goal,NewGoal,1).
  165. $transl_hardcuts1(_,_,_,L,$consult_list(L), _) :- L = [_|_], !.
  166. $transl_hardcuts1(_,_,_,L,L,_).
  167.  
  168. :- mode($transl_contains_cut,1,[nv]).
  169.  
  170. $transl_contains_cut(X) :- var(X), !, fail.
  171. $transl_contains_cut(','(A,B)) :-
  172.     $transl_contains_cut(A) ;
  173.     $transl_contains_cut(B).
  174. $transl_contains_cut(';'(A,B)) :-
  175.     $transl_contains_cut(A) ;
  176.     $transl_contains_cut(B).
  177. $transl_contains_cut('->'(A,B)) :-
  178.     $transl_contains_cut(A) ;
  179.     $transl_contains_cut(B).
  180. $transl_contains_cut(not(C)) :- $transl_contains_cut(C).
  181. $transl_contains_cut(call(C)) :- $transl_contains_cut(C).
  182. $transl_contains_cut('!').
  183.  
  184. $translcuts_naked_var_msg(P,N) :-
  185.     $umsg(['*** Warning: naked variable being called in',P,'/',N,'***']).
  186.  
  187. $transl_cuts_ifthenelse(P,N,HCut,BCut,A,B,C,if_then_else(A,NB,NC),Call) :-
  188.     $transl_all_inlines(A),
  189.     !,
  190.     $transl_hardcuts(P,N,BCut,B,NB,Call),
  191.     $transl_hardcuts(P,N,BCut,C,NC,Call).
  192. $transl_cuts_ifthenelse(P,N,HCut,BCut,A,B,C,NB,Call) :-
  193.     NB = ','('_$savecp'(HCut),';'((T1,'_$cutto'(HCut),NB0),NC)),
  194.     $transl_hardcuts(P,N,HCut,A,T1,Call),
  195.     $transl_hardcuts(P,N,BCut,B,NB0,Call),
  196.     $transl_hardcuts(P,N,BCut,C,NC,Call).
  197.  
  198. /*  at this point, a test is considered to be inline (and hence a candidate
  199.     for transformation to "if-then-else" iff it is either an inline test,
  200.     or a conjunction of inline tests, or a disjunction of inline tests.
  201.     We could set it up so that arbitrary constructs involving inlines are
  202.     allowed, but this complicates code generation quite a bit, and it's not
  203.     clear that more complex constructs are encountered frequently in
  204.     practice.                                  */
  205.  
  206. $transl_all_inlines(','(G1,G2)) :-
  207.     !,
  208.     $transl_all_inlines_conj(G1),
  209.     $transl_all_inlines_conj(G2).
  210. $transl_all_inlines(';'(G1,G2)) :-
  211.     !,
  212.     $transl_all_inlines_disj(G1),
  213.     $transl_all_inlines_disj(G2).
  214. $transl_all_inlines(A) :-
  215.     $functor(A,Pred,Arity),
  216.     $inline_test(Pred,Arity).
  217.  
  218. $transl_all_inlines_conj(','(G1,G2)) :-
  219.     !,
  220.     $transl_all_inlines_conj(G1),
  221.     $transl_all_inlines_conj(G2).
  222. $transl_all_inlines_conj(A) :-
  223.     $functor(A,Pred,Arity),
  224.     $inline_test(Pred,Arity).
  225.  
  226. $transl_all_inlines_disj(';'(G1,G2)) :-
  227.     !,
  228.     $transl_all_inlines_disj(G1),
  229.     $transl_all_inlines_disj(G2).
  230. $transl_all_inlines_disj(A) :-
  231.     $functor(A,Pred,Arity),
  232.     $inline_test(Pred,Arity).
  233.  
  234.  
  235. $find_prag(P,N,PragList,Prag) :- $member(prag(P,N,Prag),PragList), !.
  236. $find_prag(_,_,_,[]).
  237.  
  238.  
  239. :- mode($inline_test,2,[c,c]).
  240.  
  241. $inline_test('>',2).
  242. $inline_test('>=',2).
  243. $inline_test('=<',2).
  244. $inline_test('<',2).
  245. $inline_test('=:=',2).
  246. $inline_test('=\=',2).
  247. $inline_test(var,1).
  248. $inline_test(nonvar,1).
  249. $inline_test(true,0).
  250. $inline_test(integer,1).
  251. $inline_test('?=',2).
  252. $inline_test('\=',2).
  253.  
  254. /* ---------------------------- $translcuts1.P ---------------------------- */
  255.  
  256.